<?php
namespace x\my;
use x\Container;
//验证码工具类
class Captcha{
    //属性
    public static $width=80;
    public static $height=30;
    public static $fontsize=16;
    public static $pixes = true;
    public static $lines = true;
    public static $str_len = 4;
    /*
     * 产生验证码图片
    */
    public static function generate($token){
        //制作画布
        $img = imagecreatetruecolor(self::$width,self::$height);
        //给定背景色
        $bg_color = imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
        imagefill($img,0,0,$bg_color);
        //制作干扰线
        self::getLines($img);
        //增加干扰点
        self::getPixels($img);
        //增加验证码文字
        $captcha = self::getCaptcha($token);
        //文字颜色
        $str_color = imagecolorallocate($img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
        //写入文字
        //计算文字应该出现的起始位置
        $start_x = ceil(self::$width/2) - 25;
        $start_y = ceil(self::$height/2) - 8;
        if(imagestring($img,self::$fontsize,$start_x,$start_y,$captcha,$str_color)){
            //成功：输出验证码
            header('Content-type:image/png');
            imagepng($img);
        }else{
            //失败
            return false;
        }
    }
    /*
     * 获取验证码随机字符串
     * @return string $captcha，随机验证码文字
    */
    private static function getCaptcha($token){
        //获取随机字符串
        $str = implode('',array_merge(range('a','z'),range('A','Z'),range(1,9)));
        //随机取
        $captcha = '';  //保存随机字符串
        for($i = 0,$len = strlen($str);$i < self::$str_len;$i++){
            //每次随机取一个字符
            $captcha .= $str[mt_rand(0,$len - 1)] . ' ';
        }
        db()->query('delete from cookie where token=? or create_time<?',[$token,time()-30*60]);
        //将数据保存到
        $data = [
            'ip'=>ip(),
            'token'=>$token,
            'code'=>strtolower(str_replace(' ','',$captcha)),
            'type'=>'captcha',
            'create_time'=>time()
        ];
        db()->insert('cookie',$data);
        //返回值
        return $captcha;
    }
    /*
     * 增加干扰点
     * @param1 resource $img
    */
    private static function getPixels($img){
        //增加干扰点
        for($i = 0;$i < self::$pixes;$i++){
            //分配颜色
            $pixel_color = imagecolorallocate($img,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));
            //画点
            imagesetpixel($img,mt_rand(0,self::$width),mt_rand(0,self::$height),$pixel_color);
        }
    }
    /*
     * 增加干扰线
     * @param1 resource $img，要增加干扰线的图片资源
    */
    private static function getLines($img){
        //增加干扰线
        for($i = 0;$i < self::$lines;$i++){
            //分配颜色
            $line_color = imagecolorallocate($img,mt_rand(150,200),mt_rand(150,200),mt_rand(150,200));
            //画线
            imageline($img,mt_rand(0,self::$width),mt_rand(0,self::$height),mt_rand(0,self::$width),mt_rand(0,self::$height),$line_color);
        }
    }
    /*
     * 验证验证码
     * @param1 string $captcha，用户提交的验证码
     * @return bool，成功返回true，失败返回false
    */
    public static function checkCaptcha($token,$captcha){
        $rs = db()->query('select id from cookie where type=? and token=? and code=?',['captcha',$token,strtolower($captcha)],1);
        if($rs){
            db()->delete('cookie',$rs['id']);
            return true;
        }
        return false;
    }
}
